﻿/*******************************************************
*类名称：WechatImageTextManager
*版本号：V1.0.0.0
*作者：成俊杰
*CLR版本：4.0.30319.36264
*创建时间：2017-09-06 17:50:21
*说明：T_WechatImageText仓储类
*更新备注：
**********************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wechat.Domain;
using Wechat.Dto.Extension;
using Wechat.Dto.Request;
using Wechat.Dto.Response;
using Wechat.Dto.Search;
using Wechat.Util.ParmsConfig;

namespace Wechat.Dao.Implement
{
    /// <summary>
    /// T_WechatImageText
    /// </summary>
    public class WechatImageTextRepository : RepositoryBase<WechatImageText>, IWechatImageTextRepository
    {
        /// <summary>
        /// 添加图文消息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public int InsertOrUpdateImageText(WechatImageTextRequest request)
        {
            var result = 0;
            if (request.Id == 0)
            {
                var model = request.ToModel();
                model.IsEnable = true;
                model.Id = Insert(model);//添加信息
                if (model.Id > 0)
                {
                    model = Get(model.Id);//获取信息
                    model.RedictUrl =
                        string.IsNullOrWhiteSpace(request.RedictUrl) ?
                        model.RedictUrl :
                        $"{ParmsConfigUtil.WebSiteUrl}Wechat/H5News?Id={model.Id}";//修改跳转地址
                    result = Update(model) ? model.Id : 0;
                }
            }
            else
            {
                var model = this.Get(request.Id);//获取信息

                model.LastUpdateOperaterId = 0;
                model.LastUpdateTime = DateTime.Now;
                model.IsEnable = request.IsEnable;
                model.Content = request.Content;
                model.GroupId = request.GroupId;
                model.Descriptions = request.Descriptions;
                model.Title = request.Title;
                model.MsgType = request.MsgType;
                model.RedictUrl =
                    string.IsNullOrWhiteSpace(request.RedictUrl) ?
                    $"{ParmsConfigUtil.WebSiteUrl}Wechat/H5News?Id={model.Id}" :
                    model.RedictUrl;
                model.PicUrl = request.PicUrl;

                result = Update(model) ? model.Id : 0;
            }
            return result;
        }

        /// <summary>
        /// 查询图文信息
        /// </summary>
        /// <param name="request">图文信息</param>
        /// <returns></returns>
        public IList<WechatImageTextResponse> GetWechatImageTextList(WechatImageTextSearch Search)
        {
            var condition = @"
                                SELECT [Id]
                              ,[MsgType]
                              ,[Content]
                              ,[Title]
                              ,[Descriptions]
                              ,[PicUrl]
                              ,[RedictUrl]
                              ,[GroupId]
                              ,[IsEnable]
                                FROM [Wechat].[dbo].[T_WechatImageText] where 1=1 AND ( IsDeleted=0 OR IsDeleted IS NULL ) ";

            var strWhere = string.Empty;
            if (Search != null)
            {
                if ((Search.Id ?? 0) > 0)
                    strWhere = $" AND Id={Search.Id}";

                if ((Search.GroupId ?? 0) > 0)
                    strWhere = $" AND GroupId={Search.GroupId}";
            }
            var imageTextList = LoadByCondition(condition + strWhere, null)
                .Select(image => image.ModelTo())
                .ToList();
            return imageTextList;
        }
    }
}